package light.head.controller.mobile;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.mobile.MobileLeaveManageDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.LeaveManage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 手机版 请假申请
 * @author yangguanghe @date 2014-5-27 @time 下午3:52:59
 * 
 * 
 * 目前我并没有有使用这个类(wz)
 */
@Controller
@RequestMapping(Url.MOBILE_LEAVEAPPLY)
public class MobileLeaveApplyController extends SupporterController {

	private MobileLeaveManageDAO mlmDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init (HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("手机版 请假申请yangguanghe");
		return new ModelAndView(Url.MOBILE_LEAVEAPPLY_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query (HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED, Parm.ST, Parm.ET, Parm.CUNAME);
		map.put(Parm.CU, cu_code(request).toString());
		PageBean pb = createPageBean(request);
		mlmDAO.findByMap(cu_organCode(request), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MOBILE_LEAVEAPPLY_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		
		if (isNotNull(code)) {
			LeaveManage lm = mlmDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INFO, lm);
		}
		return new ModelAndView(Url.MOBILE_LEAVEAPPLY_EDIT);
	}
	
	@RequestMapping(Url.SAVE)
	public void save (LeaveManage lm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (null == lm) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		RESULT result = RESULT.ERROR;
		if (null == lm.getCode()) {
			lm.setCu(cu_code(request));
			lm.setCt(new Date());
			lm.setOc(cu_organCode(request));
			lm.setDeleted(false);
			result = mlmDAO.add(lm);
		} else {
			result = mlmDAO.update(lm); 
		}
		
		if (RESULT.SUCCESS == result) 
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
		
	}
	
	@RequestMapping(Url.SHIELD)
	public void shield (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			LeaveManage lm = mlmDAO.get(Integer.parseInt(code));
			if (null == lm) {
				print(response, Msg.NOT_FIND);
				return;
			}
			
			lm.setDeleted(true);
			if (RESULT.SUCCESS == mlmDAO.upd(lm)) {
				print(response, Msg.SUCCESS);
			}else {
				print(response, Msg.ERROR);
			}	
			return ;
		}
		print(response, Msg.ERROR);
	}

	
	@Autowired
	public void setMlmDAO(MobileLeaveManageDAO mlmDAO) {
		this.mlmDAO = mlmDAO;
	}
	
	
	
}
